<template>
	<uni-forms ref="formRef" :model="formData" :rules="formRules" class="p10 forms-dom" :label-width="80">
		<uni-forms-item label="咨询专家" required>
			<view style="line-height: 36px; padding-left: 10px;">{{formData.receiveExpertName}}</view>
		</uni-forms-item>
		<uni-forms-item label="咨询目的" name="consultationPurpose" required>
			<uni-easyinput v-model="formData.consultationPurpose" placeholder="请输入咨询目的" trim />
		</uni-forms-item>
		<uni-forms-item label="咨询时间" name="consultationTime" required>
			<view class="inputtest-dom">
				<uni-easyinput v-model="formData.consultationTime"  placeholder="请输入选择预约时间" trim :clearable="false"/>
				<view class="empty" @click="visible = true"></view>
			</view>
			<my-date-picker :visible.sync="visible"  @confirmPickDate="confirmPickDate" />
		</uni-forms-item>
		<uni-forms-item label="联系电话" name="phone" required>
			<uni-easyinput v-model="formData.phone" type="number" placeholder="请输入联系电话" trim />
		</uni-forms-item>
		<uni-forms-item label="具体问题" name="concreteProblems" required>
			<uni-easyinput v-model="formData.concreteProblems" placeholder="请输入具体问题" type="textarea" autoHeight trim />
		</uni-forms-item>
	</uni-forms>
</template>

<script lang="ts" setup>
	import myDatePicker from '@/components/uni/date-picker.vue';
	import { getUser } from "@/utils/storage"
	import { User } from "@/api/tables/User"
	import { consultEdit, consultAdd } from "@/api/consult"
	import { ref, onBeforeMount } from 'vue'
	const emit = defineEmits(["formCheck"])
	let userData = ref<User>({})
	let visible = ref(false)
	const props = defineProps({
		formData: {
			type: Object,
			default: function () {
				return {
				};
			}
		}
	})
	const formRules = ref({
		consultationTime: {
			rules: [{
				required: true,
				errorMessage: '请输入咨询时间'
			}]
		},
		consultationPurpose: {
			rules: [{
				required: true,
				errorMessage: '请输入咨询目的'
			}]
		},
		concreteProblems: {
			rules: [{
				required: true,
				errorMessage: '请输入咨询具体内容'
			}]
		},
		phone: {
			rules: [{
				required: true,
				errorMessage: '联系电话不能为空'
			}, {
				pattern: /^[1][3,4,5,7,8][0-9]{9}$/,
				errorMessage: '联系电话 格式错误',
			}],
		}
	})
	let formRef = ref(null)

	onBeforeMount(() => {
		userData.value = getUser()
	})
	function formCheck() {
		formRef.value.validate().then(res => {
			// subscribe ==1 // 已开启信息订阅
			if (userData.value.subscribe == 1) {
				// #ifdef MP-WEIXIN 
				uni.getSetting({
					withSubscriptions: true,
					success: (res) => {
						if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
							if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
								uni.requestSubscribeMessage({
									tmplIds: ['soYq1ZBYywTGeDvPttbgmb5_lYPGnu3k0zozDcyXJJI'],
									success: () => { sontrolsOk() }
								});
							} else {
								uni.requestSubscribeMessage({
									tmplIds: ['soYq1ZBYywTGeDvPttbgmb5_lYPGnu3k0zozDcyXJJI'],
									success: () => { sontrolsOk() }
								});
							}
						} else { sontrolsOk() }
					},
					fail: () => { sontrolsOk() }
				});
				// #endif

				// #ifdef H5 || APP-PLUS 
				sontrolsOk()
				// #endif
			}else{
				sontrolsOk()
			}

		}).catch(err => {
			uni.showToast({ icon: "none", title: err[0].errorMessage })
		})
	}
	function sontrolsOk() {
		let id = props.formData.id
		if (id) {
			editOk()
		} else {
			addOK()
		}
	}
	function editOk() {
		consultEdit({
			...props.formData
		}).then(res => {
			if (res.code == '200') {
				uni.$emit('editUpdate', props.formData)
				uni.navigateBack({
					delta: 1
				})
			}
		})
	}
	function addOK() {
		consultAdd({
			...props.formData
		}).then(res => {
			if (res.code == '200') {
				uni.redirectTo({
					url: './add-upload?itemID=' + res.data.id
				});
			}
		})
	}
	function confirmPickDate(dateStr) {
		props.formData.consultationTime = dateStr
		visible.value = false
	}
	defineExpose({
		formCheck
	})
</script>

<style lang="scss" scoped>
	.inputtest-dom{
			position: relative;
		 
			.empty {
				z-index: 99;
				width: 100%;
				height: 100%;
				position: absolute;
				left: 0;
				top: 0;
			}
	}
</style>